package com.owen.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.owen.blog.dao.BlogLabelDao;
import com.owen.blog.domain.BlogLabel;
import com.owen.blog.dto.BlogLabelDTO;
import com.owen.blog.service.BlogLabelService;
import com.owen.blog.vo.BlogLabelVO;
import com.owen.common.utils.ShiroUtils;
import com.owen.common.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 店铺管理服务实现
 */
@Service
public class BlogLabelServiceImpl implements BlogLabelService {

    @Autowired
    private BlogLabelDao blogLabelDao;

    // 雪花算法生成id
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Override
    public BlogLabelVO get(String id) {
        return blogLabelDao.get(id);
    }

    @Override
    public List<BlogLabelVO> list(Map<String, Object> map) {
        return blogLabelDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return blogLabelDao.count(map);
    }

    @Override
    public int save(BlogLabelDTO blogLabel) {
        String userId = ShiroUtils.getUserId();
        Assert.isTrue(ObjectUtil.isNotEmpty(userId), "请登录");
        BlogLabel bean = BeanUtil.toBean(blogLabel, BlogLabel.class);
        bean.setId(snowflakeIdWorker.nextIdStr());
        bean.setCreateId(userId);
        return blogLabelDao.save(bean);
    }

    @Override
    public int update(BlogLabelDTO blogLabel) {
        String userId = ShiroUtils.getUserId();
        Assert.isTrue(ObjectUtil.isNotEmpty(userId), "请登录");
        BlogLabel bean = BeanUtil.toBean(blogLabel, BlogLabel.class);
        bean.setCreateId(userId);
        return blogLabelDao.update(bean);
    }

    @Override
    public int remove(String id) {
        return blogLabelDao.remove(id);
    }

    @Override
    public int batchRemove(String[] ids) {
        return blogLabelDao.batchRemove(ids);
    }

    @Override
    public List<BlogLabelVO> findAll() {
        return blogLabelDao.findAll();
    }
}
